package com.health.mapp.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.MedicalTreatRecordTemp;
import com.health.db.model.MedicalTreatRecordWithBLOBs;
import com.health.db.model.PatientList;
import com.health.db.model.RegisterOrder;
import com.health.db.model.RegisterOrderExample;
import com.health.db.model.RegisterOrderTemp;
import com.health.db.model.UserLIST;
import com.health.db.service.order.IRegisterOrderService;
import com.health.db.service.sm.IRongService;
import com.health.db.service.user.IGoodsPriceListService;
import com.health.db.service.user.IPatientListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.UpdateOrderReq;
import com.health.mapp.bean.UpdateOrderResp;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 *  预订单生成查询类
 *  首儿送药
 * @author HiRoad
 * 
 * @pramati
 * 	private Long patId;//	String(Long)	Y	就诊人ID
	private Long userId	;//String(Long)	Y	用Id
	private int orderType;//	Int 	Y	订单类型 1:图文咨询 2:电话咨询 3:视频咨询 4：首尔送药
	private int freeType;//	Int	Y	是否免费咨询，默认为是。0：是 ；1：否
	private String goodsId	;//	String N	非免费咨询的时候必传
	private int channel	;//Int	Y	来源渠道
	private String contactTel	;//String	N	联系电话
	private String scheduleTime	;//String(Time)	N	预约时间，免费咨询传空或默认值2017-02-21 13:00 
	private String scheduleDate;//	String(Time)	N	预约时间，免费咨询传空或默认值2017-02-21
	private int orderTimeSolt ;//	Int	N	时间节点
	private String orderTime	;//String(Time)	N	预订时间长，免费咨询传空或默认值
	private Long treatDocId;//	Long	N	就诊医师ID，免费咨询传空或默认值
	private String treatDocName;//	String	N	就诊医师姓名，免费咨询传空或默认值
	private String departmentName	;//String	N	科室名称，免费咨询传空或默认值
	private int departmentId;//	int	N	科室ID，免费咨询传空或默认值
	private int hospitalId;//	Int	Y	医院编号，默认为2
	private int patInsertMode	;//Int	N	病人自述插入方式空或者0：
	private String patRecord ;//未预先生成，需要插入patRecord对象   //1：patRecord已经预先生成，只需要插入实现生成的patRecordId
	private String patRecordId	;//Int	N	patInsertMode=1，必传
	private String patDescribe	;//String	N	病人自述  patInsertMode=0或者空，必传
	private String patPage1	;//String,File	N	病人上传图片路径，文件上传
	private String patPage2	;//String,File	N	病人上传图片路径，文件上传
	private String patPage3	;//String,File	N	病人上传图片路径，文件上传
	private String patPage4	;//String,File	N	病人上传图片路径，文件上传
	private String patPage5	;//String,File	N	病人上传图片路径，文件上传
	private String patPage6	;//String,File	N	病人上传图片路径，文件上传
 *
 */
public class UpdateOrderImpl implements ActionInterface {
	
	//service import
	private IRegisterOrderService registerOrderService  ;
	private IUserLISTService userLISTService  ;
	private IPatientListService patientListService  ;
	private IGoodsPriceListService goodsPriceListService  ;
	private IRongService rongService  ; 
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		UpdateOrderReq req = (UpdateOrderReq) reqBean; 
		UpdateOrderResp _resp = new UpdateOrderResp();
		Long patientId=req.getPatId();
		Long orderId = req.getOrderId();
		if (orderId==0) {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		else
		{
			 //获取当前要处理的订单
			RegisterOrder  currRegisterOrder =registerOrderService.selectSimpleSingleByOrderId(orderId);
			
			//判断订单状态
			if (currRegisterOrder==null) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
				return _resp;
			}
			//RegisterOrder 包装
			RegisterOrder orderInfo = new RegisterOrder();
			
			orderInfo.setPatientid(patientId);
			orderInfo.setRegisterDesc(req.getPatDescribe());

			//查看用户表相关信息
			UserLIST userLIST = userLISTService.selectByUserId(currRegisterOrder.getUserid());
			
			orderInfo.setUserName(userLIST.getUserName());
			orderInfo.setUserContact(userLIST.getUserNo());
			
			PatientList patient=patientListService.selectByPatientId(patientId);
			if(null!=patient)
			{
				orderInfo.setPatientName(patient.getPatientName());
			}else{
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
			
			Date currDateTime=DateUtils.getDatetime();
			
			//第一次生成订单，更新时间和创建时间一致
			
			orderInfo.setUpdateDate(currDateTime);
			//病历数据包装
			MedicalTreatRecordWithBLOBs patRecord = new MedicalTreatRecordWithBLOBs();
			patRecord.setBillNo(DateUtils.todayDate()+"-H"+currRegisterOrder.getEntityid()+"P"+req.getPatId()+"-");
			patRecord.setUserid(currRegisterOrder.getUserid());
			patRecord.setPatientid(req.getPatId());
			patRecord.setHospitalid(Long.valueOf(currRegisterOrder.getEntityid()));
			patRecord.setDepartmentid(Long.valueOf(currRegisterOrder.getDepartmentid()));
			patRecord.setDepartmentName(currRegisterOrder.getDepartmentName());
			if(StringUtils.isNotBlank(req.getScheduleTime())){
				patRecord.setScheduleTime(DateUtils.parseDatetime(req.getScheduleTime(), "yyyy-MM-dd HH:mm"));
			}
			patRecord.setPatientDescribe(req.getPatDescribe());
			patRecord.setPatpage1(req.getPatPage1());
			patRecord.setPatpage2(req.getPatPage2());
			patRecord.setPatpage3(req.getPatPage3());
			patRecord.setPatpage4(req.getPatPage4());
			patRecord.setPatpage5(req.getPatPage5());
			patRecord.setPatpage6(req.getPatPage6());
			patRecord.setDoctorid(currRegisterOrder.getAccepterid());
			patRecord.setDoctorName(currRegisterOrder.getAccepterName());
			//记录更新人的姓名
			patRecord.setUpdateUserid(currRegisterOrder.getUserid());
 
			//返回参数
			RegisterOrderExample temp = registerOrderService.updateOrder(orderInfo, patRecord);
			
			//订单约满
			if (temp.getUpdateNum()==Constant.Schedule_Flag) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3008);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3008_NOTE);
				return _resp;
			}
			
			//包装返回参数 主要未订单号，订单状态
			RegisterOrderTemp returnOrderInfo = new RegisterOrderTemp();
			returnOrderInfo.setOrderId(String.valueOf(temp.getRegisterid()));
			returnOrderInfo.setOrderState(String.valueOf(orderInfo.getRunstatusid()));
		    MedicalTreatRecordTemp returnPatRecord = new MedicalTreatRecordTemp();
		    returnPatRecord.setPatRecordId(String.valueOf(temp.getRefbillid()));
		    returnOrderInfo.setPatRecord(returnPatRecord);
		    returnOrderInfo.setOrderFee((orderInfo.getRegisterTotal())==null?"0":String.valueOf(orderInfo.getRegisterTotal()));
			_resp.setOrderInfo(returnOrderInfo);
			
			orderInfo.setRegisterid(temp.getRegisterid());
		}
		
		
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}
	
	public IRegisterOrderService getRegisterOrderService() {
		return registerOrderService;
	}


	public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
		this.registerOrderService = registerOrderService;
	}


	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}


	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}


	public IPatientListService getPatientListService() {
		return patientListService;
	}


	public void setPatientListService(IPatientListService patientListService) {
		this.patientListService = patientListService;
	}


	public IGoodsPriceListService getGoodsPriceListService() {
		return goodsPriceListService;
	}


	public void setGoodsPriceListService(
			IGoodsPriceListService goodsPriceListService) {
		this.goodsPriceListService = goodsPriceListService;
	}


	public IRongService getRongService() {
		return rongService;
	}


	public void setRongService(IRongService rongService) {
		this.rongService = rongService;
	}
	
	
	
}
